package com.spark.userservice.controller;


import com.alibaba.fastjson.JSON;
import com.spark.basecommon.api.R;
import com.spark.basecommon.entity.user.UmsAdmin;
import com.spark.basecommon.entity.vo.LoginUserVO;
import com.spark.userapi.vo.AccountVO;
import com.spark.userservice.service.IUmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author fuqiang
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/user")
@Api(tags = "后台用户管理")
@AllArgsConstructor
public class UmsAdminController {

    private IUmsAdminService umsAdminService;

    private UmsAdmin getCurrentUser(){
        //获取用户身份信息
        UmsAdmin user = (UmsAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user;
    }

    @GetMapping(value = "/test")
    @PreAuthorize("hasAuthority('pms:product:read')")
    public R<UmsAdmin> r1(){
        //获取用户身份信息
        UmsAdmin currentUser = getCurrentUser();
        return R.success(currentUser);
    }

    /**
     * 用户登录
     * @param accountVO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    public R<String> login(@Validated @RequestBody AccountVO accountVO){
        String token = umsAdminService.login(accountVO.getUsername(),accountVO.getPassword());
        return R.success(token);
    }

    @PostMapping("/getUserInfoByUserName")
    @ApiOperation("根据userName 查询用户信息")
    public R<LoginUserVO> getUserInfoByUserName(@RequestBody String username){
        return R.success(umsAdminService.getUserInfoByUserName(username));
    }
}
